/******************************************************************************
* CHIPSEA F61 TIMER HAL
*******************************************************************************/

#include "hal_aw_86802_timer.h"
#include "algorithm_init.h"

#ifdef AW_86802

#include <stdint.h>
#include <stdbool.h>

/*******************************************************************************
@brief : 配置CPU时钟为36M
@param : NONE
@ret   : NONE
*******************************************************************************/
void Config_HCLK_36M(void)
{

}

void aw_pwr_init(void)
{
	PWR_HANDLE_TYPE_S pwr_init_s;

	pwr_init_s.pwr_irq_pri_e = AW_PRIORITY_2;
	pwr_init_s.init_s.pwr_dp_flash = DP_FLASH_EN;
	pwr_init_s.init_s.pwr_mode_e = DP_SLEEP;
	pwr_init_s.init_s.pwr_load_cent = SWDT_LOAD_10MS;

	pwr_init(&pwr_init_s);

	protect_unlock();
	PWR->SWDT_EN = 0x01; // open swdt time
	protect_lock();
}


u16 afe_timeout_cnt = 0;

void SWDt_IRQnHandler(void)
{
	// GPIO_WriteBits(GPIO_PIN_10, 1);

	pwr_clean_irq();

	st_hal_platform.timer_flag[0] = true;
	afe_timeout_cnt++;

	// CLK->ISP_CLK_EN = 0; // to save power 

	I2C0->ENABLE = I2C_ENABLE_EN;
	PWR->SWDT_EN = 0x01; // open swdt time

	// GPIO_WriteBits(GPIO_PIN_10, 0);
}

// AW_U8 nvr_dat[NVR_BUFF_LEN] = {00,00,00,00};
// void aw_flash_start_fun()
// {
// 	//AW_U8 nvr_dat[NVR_BUFF_LEN] = {00,00,00,00};

// 	protect_unlock();
// 	CLK->ISP_CLK_EN |= CFG_CLK_EN;
// 	protect_lock();
// 	flash_read_many_nvr(NVR_ADR_1, nvr_dat, NVR_BUFF_LEN);
// 	if (nvr_dat[8] == LOSC_10K_TRIM) {
// 		flash_erase_nvr(NVR_ADR_2);
// 		flash_write_many_nvr(NVR_ADR_2, nvr_dat, NVR_BUFF_LEN);
// 		nvr_dat[8] = LOSC_35K_TRIM;
// 		flash_erase_nvr(NVR_ADR_1);
// 		flash_write_many_nvr(NVR_ADR_1, nvr_dat, NVR_BUFF_LEN);
// 	}

// 	FMC->ISP_CMD = ISPCMD_DIS;
// }

// AW_U8 nvr_dat1[NVR_BUFF_LEN];
// AW_U8 nvr_dat2[NVR_BUFF_LEN];
// AW_U8 nvr_dat3[NVR_BUFF_LEN];
// #define NVR_ADR_3		(0x02000600)

// void dbg_read_nvr_arr(void)
// {
// 	CLK->ISP_CLK_EN |= CFG_CLK_EN;
// 	flash_read_many_nvr(NVR_ADR_1, nvr_dat1, NVR_BUFF_LEN);
// 	flash_read_many_nvr(NVR_ADR_2, nvr_dat2, NVR_BUFF_LEN);
// 	flash_read_many_nvr(NVR_ADR_3, nvr_dat3, NVR_BUFF_LEN);
// }

void aw_trim_check_fun(void)
{
	AW_U8 sys_get_nvr_losc = 0;
	AW_U8 sys_get_nvr_hosc = 0;
	AW_U8 sys_get_nvr_temp = 0;
	AW_U8 sys_get_nvr_ldo = 0;
	// AW_U8 sys_get_nvr_prog = 0; 
	AW_U32 sys_get_nvr_trim = 0;

	protect_unlock();
	CLK->ISP_CLK_EN |= CFG_CLK_EN;
	
	sys_get_nvr_trim = SYS->RESERVED1[0]; // get HOSC LOSC TEMP LDO PROG trim. 
	
	sys_get_nvr_losc = (sys_get_nvr_trim & 0xFF);
	sys_get_nvr_hosc = ((sys_get_nvr_trim & 0xFF00) >> 8);
	sys_get_nvr_temp = ((sys_get_nvr_trim & 0xFF0000) >> 16);
	sys_get_nvr_ldo = ((sys_get_nvr_trim & 0xF000000) >> 24);
	// sys_get_nvr_prog = ((sys_get_nvr_trim & 0xF0000000) >> 28);
	if (sys_get_nvr_losc == LOSC_10K_TRIM) {
		SYS->RESERVED2[7] = LOSC_35K_TRIM; // LOSC trim write. 0x60
		SYS->RESERVED2[8] = sys_get_nvr_hosc; // HOSC trim write. 0x64
		SYS->RESERVED2[0] = sys_get_nvr_temp; // temp trim write. 0x44
		SYS->RESERVED2[9] = sys_get_nvr_ldo; // LDO trim write. 0x68
		SYS->CFG_MOD = 0xFFFF00FD; // configuration choice from register and start flash
	}
	protect_lock();
}

// void aw_flash_start_fun()
// {
// 	AW_U8 read_many_nvr[64] = {0};
// 	AW_U8 nvr_write_data[64] = {   0x02,0x68,0x08,0x00,
// 	0x04,0x07,0x14,0x00,
// 	0x1C,0xA4,0x00,0xAF,
// 	0x30,0x00,0x00,0x69,
// 	0x23,0x20,0x00,0x00,
// 	0xAA,0x55,0x5A,0x5A};
// 	protect_unlock();
// 	CLK->ISP_CLK_EN |= CFG_CLK_EN;
// 	protect_lock();
// 	flash_init();
// 	flash_erase_nvr(NVR_ADR_1); // 擦除NVR扇区中的数据
// 	flash_write_many_nvr(NVR_ADR_1, nvr_write_data, 64); //量产数据写入
// 	flash_read_many_nvr(NVR_ADR_1, read_many_nvr, 64);//读取写入的数据
// }

void hosc_config(void)
{
	// aw_flash_start_fun();
	// dbg_read_nvr_arr();
	aw_trim_check_fun();
	protect_unlock();
	SYS->CFG_MOD |= 1; // flash star--------------------modify
	CLK->ADC_CLK_EN = 0;
	CLK->AFE_CLK_EN = 0;
	CLK->WDT0_CLK_EN = 0;
	CLK->WDT1_CLK_EN = 0;
	CLK->WDT2_CLK_EN = 0;
	CLK->I2C0_CLK_EN = 0;
	CLK->CRC_CLK_EN = 0;
	CLK->HDIV_CLK_EN = 0;
	CLK->UART_CLK_EN = 0;
	CLK->TMR0_CLK_EN = 0;
	CLK->TMR1_CLK_EN = 0;
	CLK->ISP_CLK_EN = 0;
	RST->PDR_EN = 0;

	protect_lock();
}

/********************************************************
@brief : TIMER0初始化，LIRC(10KHZ)作为时钟源
@param : tcmp : 定时器计数值
@ret   : none
@tips  : none
********************************************************/
void User_Timer0_Init()
{

}

void User_Timer1_Init()
{

}

/********************************************************************************
@brief : TIMER0中断 1ms
@param : NONE
@ret   : NONE
********************************************************************************/
void TMR0_IRQHandler(void)
{

}

////////////////////////////////////////////////////////////////////////////////////////////////////

void hal_timer_reset(uint8_t timer, uint32_t time)
{
	u16 tm = time * 2;

	pwr_swdt_load_fun(tm); // Config sleep Wake-up time

	PWR->SWDT_EN = 0x01; // open swdt time

}

////////////////////////////////////////////////////////////////////////////////////////////////////

#endif 
